package com.csair.common.helper;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;

import com.csair.common.CommonApplication;
import com.csair.common.utils.LanguageUtil;

import java.util.ArrayList;
import java.util.List;

public class PermissionActivity extends AppCompatActivity {
    public static final String ACCESS_FINE_LOCATION = "android.permission.ACCESS_FINE_LOCATION";//允许一个程序访问精良位置(如GPS)
    public static final String CALL_PHONE = "android.permission.CALL_PHONE";//允许一个程序初始化一个电话拨号
    public static final String GET_TASKS = "android.permission.GET_TASKS";//允许程序获取当前或最近运行的应用
    public static final String READ_CONTACTS = "android.permission.READ_CONTACTS";//允许程序读取用户联系人数据
    public static final String READ_EXTERNAL_STORAGE = "android.permission.READ_EXTERNAL_STORAGE";//允许程序读取外部存储空间
    public static final String WRITE_EXTERNAL_STORAGE = "android.permission.WRITE_EXTERNAL_STORAGE";//允许程序写入外部存储空间
    public static final String WRITE_SETTINGS = "android.permission.WRITE_SETTINGS";//允许程序读取或写入系统设置
    public static final String CAMERA = "android.permission.CAMERA";//请求访问使用照相设备
    public static final String READ_CALENDAR = "android.permission.READ_CALENDAR";
    public static final String WRITE_CALENDAR = "android.permission.WRITE_CALENDAR";
    public static final String READ_PHONE_STATE = "android.permission.READ_PHONE_STATE";

    private static List<String> requestPermission;
    private static GrandPermissionListrner grandPermissionListrner;
    private static RefuseGrandPermissionListener refuseGrandPermissionListener;

    public static void checkPermission(GrandPermissionListrner listrner,
                                       String... permission) {
        requestPermission = new ArrayList<>();

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M ||
                permission == null || permission.length == 0 ||
                isAllPermissionGranded(permission)) {
            if (listrner != null) listrner.grandPermissionListrner();
            return;
        }

        grandPermissionListrner = listrner;
        Intent intent = new Intent(CommonApplication.getApplication(), PermissionActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        CommonApplication.getApplication().startActivity(intent);
    }

    public static void checkPermission(GrandPermissionListrner listrner,
                                       RefuseGrandPermissionListener rListener,
                                       String... permission) {
        requestPermission = new ArrayList<>();

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M ||
                permission == null || permission.length == 0 ||
                isAllPermissionGranded(permission)) {
            if (listrner != null) listrner.grandPermissionListrner();
            return;
        }

        grandPermissionListrner = listrner;
        refuseGrandPermissionListener = rListener;
        Intent intent = new Intent(CommonApplication.getApplication(), PermissionActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        CommonApplication.getApplication().startActivity(intent);
    }

    /**
     * 判断权限是否已经全部被授予
     * 未授予的保存在静态变量中
     */
    private static boolean isAllPermissionGranded(String... permission) {
        boolean isAllGrand = true;
        for (String per : permission) {
            if (!BasicFunctions.checkPermission(per)) {
                requestPermission.add(per);
                isAllGrand = false;
            }
        }
        return isAllGrand;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setTitle(" ");

        if (requestPermission == null || requestPermission.size() == 0) {
            finish();
            return;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
            requestPermissions(requestPermission.toArray(new String[requestPermission.size()]), 200);
    }

    @Override
    protected void onDestroy() {
        requestPermission = null;
        grandPermissionListrner = null;
        refuseGrandPermissionListener = null;
        super.onDestroy();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 200) {
            if (hasAllPermissionGranted(grantResults)) {
                if (grandPermissionListrner != null)
                    grandPermissionListrner.grandPermissionListrner();
            } else if (refuseGrandPermissionListener != null) {
                refuseGrandPermissionListener.refuseGrandPermissionListener();
            }
        }
        super.finish();
    }

    /**
     * 获取全部权限
     */
    private boolean hasAllPermissionGranted(int[] grantResults) {
        for (int grantResult : grantResults) {
            if (grantResult == PackageManager.PERMISSION_DENIED) {
                return false;
            }
        }
        return true;
    }

    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(LanguageUtil.refreshContext(newBase));
    }

    public interface GrandPermissionListrner {
        void grandPermissionListrner();
    }

    public interface RefuseGrandPermissionListener {
        void refuseGrandPermissionListener();
    }

}
